package per.jdz.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
import per.jdz.demo.domain.entity.HouseData;
import per.jdz.demo.domain.entity.TempHouse;
import per.jdz.demo.domain.vo.reponse.ApiResult;
import per.jdz.demo.service.HouseDataService;
import per.jdz.demo.service.TempHouseService;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author Jdz
 * @date 2025/1/14
 */
@RestController
@Slf4j
@RequestMapping("/api")
public class HouseDataController {

    @Resource
    private HouseDataService houseDataService;
    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor executor;
    @Resource
    private TempHouseService tempHouseService;

    @PostMapping("/geo")
    public ApiResult updateGeo(@RequestBody TempHouse houseData) {
        log.info("接收数据");
//        log.info("{}", houseData.get(0));
//        CompletableFuture.runAsync(() -> houseDataService.updateBatch(houseData, houseData.size()), executor)
//                .whenCompleteAsync((v, t) -> {
//                    if (t == null) log.info("更新成功");
//                    else log.error("更新失败{}", t.getMessage());
//                });
//        houseDataService.updateBatch(houseData, houseData.size());
        CompletableFuture.runAsync(() -> tempHouseService.save(houseData), executor)
                .whenCompleteAsync((v, t) -> {
                    if (t == null) log.info("更新成功");
                    else log.error("更新失败{}", t.getMessage());
                });
        return ApiResult.success();
    }

    @GetMapping("/data/{id}")
    public ApiResult getHouseData(@PathVariable Long id) {
        CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
        }, executor).whenCompleteAsync((v, t) -> {
            if (t == null) log.info("更新成功");
            else log.error("更新失败{}", t.getMessage());
        });
        return ApiResult.success();
    }
}
